import type { FormInstance, FormItemRule } from 'element-plus'

import { E_YN } from '@/config/enums/base'

interface IForm {
  username: string
  email: string
  password: string
  ns1: string
  ns2: string
  group_id: string
  remark: string
  status: number
  api_status: number
}

export default function useEditUserForm() {
  const formRef = ref<FormInstance>()

  const formData = reactive<IForm>({
    username: '',
    password: DEFAULT_PASSWORD,
    email: '',
    ns1: '',
    ns2: '',
    group_id: '0',
    remark: '',
    status: E_YN.Y,
    api_status: E_YN.N
  })

  const formRules = reactive<Record<string, FormItemRule[]>>({
    username: [
      { required: true, message: '请输入账号' },
      { min: 4, max: 18, message: '账号长度为4-18' },
      // 可以包括英文字母、数字、下划线、@符号；
      { pattern: /^[a-zA-Z0-9_@]{4,18}$/, message: '账号格式错误' }
    ],
    password: [
      { required: true, message: '请输入密码' },
      {
        trigger: 'blur',
        validator: passwordValidate
      }
    ],
    remark: [{ max: 200, message: '最多输入200个字符' }],
    email: [
      { required: true, message: '请输入邮箱' },
      { type: 'email', message: '邮箱格式错误' }
    ],
    ns1: [{ required: true, message: '请选择DNS地址' }],
    ns2: [{ required: true, message: '请选择DNS地址' }]
  })

  return {
    formRef,
    formData,
    formRules
  }
}
